package com.zhiwen.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.zhiwen.common.api.Result;
import com.zhiwen.domain.dto.ShareDTO;
import com.zhiwen.service.ShareService;
import com.zhiwen.domain.vo.ShareVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;

/**
 * 分享控制器
 */
@Tag(name = "内容分享", description = "内容分享相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/shares")
public class ShareController {

    private final ShareService shareService;

    /**
     * 创建分享
     *
     * @param shareDTO 分享信息
     * @param request HTTP请求
     * @return 分享详情
     */
    @Operation(summary = "创建分享", description = "创建内容分享，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "创建成功",
                content = @Content(schema = @Schema(implementation = ShareVO.class)))
    @SaCheckLogin
    @PostMapping
    public Result<ShareVO> createShare(@Valid @RequestBody ShareDTO shareDTO, HttpServletRequest request) {
        ShareVO shareVO = shareService.createShare(shareDTO, request);
        return Result.success(shareVO);
    }

    /**
     * 获取分享详情
     *
     * @param id 分享ID
     * @return 分享详情
     */
    @Operation(summary = "获取分享详情", description = "根据分享ID获取内容详情")
    @ApiResponse(responseCode = "200", description = "获取成功",
                content = @Content(schema = @Schema(implementation = ShareVO.class)))
    @GetMapping("/{id}")
    public Result<ShareVO> getShareDetail(@Parameter(description = "分享ID") @PathVariable Long id) {
        ShareVO shareVO = shareService.getShareDetail(id);
        return Result.success(shareVO);
    }

    /**
     * 获取分享链接
     *
     * @param shareDTO 分享信息
     * @return 分享链接
     */
    @Operation(summary = "获取分享链接", description = "生成内容分享链接，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "生成成功")
    @SaCheckLogin
    @PostMapping("/link")
    public Result<String> generateShareLink(@Valid @RequestBody ShareDTO shareDTO) {
        String shareLink = shareService.generateShareLink(shareDTO);
        return Result.success(shareLink);
    }
}
